package manager;

/**
 * @author huangjm.2012, mohameds.2012, jaslin.wee.2012, huiwensong.2012, sheryl.tay.2012
 * 
 */

import manager.database.DatabaseMgr;
import manager.entitycreator.RoundCreator;
import entity.Round;

public class RoundMgr {

	private DatabaseMgr dbMgr = new DatabaseMgr();
	private RoundCreator roundCreator = new RoundCreator();

	public boolean stopRound(int roundNo) {
		String sql = "update round set round_endDatetime=now() where round_roundNo=?";
		int numRowsAffected = dbMgr.executeUpdate(sql, roundNo);
		return numRowsAffected > 0;
	}

	public void reset() {
		String sql = "update round set round_startDatetime=null, round_endDatetime=null";
		dbMgr.executeUpdate(sql);
		
		sql = "delete from section_min_bid_amount";
		dbMgr.executeUpdate(sql);
	}

	public Round retrieveCurrentRound() {
		String sql = "select * from current_round_with_isopen ";
		return dbMgr.retrieveEntityForQuery(roundCreator, sql);
	}

	public boolean startRound(int roundNo) {
		String sql = "update round set round_startDatetime=now() where round_roundNo=?";
		int numRowsAffected = dbMgr.executeUpdate(sql, roundNo);
		return numRowsAffected > 0;
	}

	public double retrieveMinBidAmountForSection(String courseCode,
			String sectionCode) {
		String sql = "select section_min_bid_amount_minBidAmount from section_min_bid_amount "
				+ " where section_min_bid_amount_courseCode=? and section_min_bid_amount_sectionCode=? ";
		return dbMgr.retrieveSingleDoubleForQuery(sql, courseCode, sectionCode);
	}

	public boolean updateMinBidAmountForSection(double minBidAmount,
			String courseCode, String sectionCode) {
		String sql = "replace into section_min_bid_amount "
				+ " (section_min_bid_amount_courseCode, section_min_bid_amount_sectionCode, section_min_bid_amount_minBidAmount) "
				+ " values (?,?,?) ";
		return dbMgr.executeUpdate(sql, courseCode, sectionCode, minBidAmount) > 0;
	}

}
